<?php  
## include required files
/*******************************/
require_once '../model/blogs.php';
require_once '../model/users.php';
require_once '../model/moduleuser.php';
## Paginaton class
require_once ("../includes/classes/pagination_class.php");

function order_array_num ($array, $key, $order = "ASC")
    {
        $tmp = array();
        foreach($array as $akey => $array2)
        {
            $tmp[$akey] = $array2[$key];
        }
       
        if($order == "DESC")
        {arsort($tmp , SORT_NUMERIC );}
        else
        {asort($tmp , SORT_NUMERIC );}

        $tmp2 = array();       
        foreach($tmp as $key => $value)
        {
            $tmp2[$key] = $array[$key];
        }       
       
        return $tmp2;
    } 


/*******************************/
## Create Objects
/*******************************/
$blogObj = new Model_Blogs();
$userObj = new Model_Users();
$moduleObj=new Model_ModuleUser();
/*******************************/
## Active, Inactive selected records
/*******************************/
if(isset($_POST['go']) != '' && isset($_POST['action']) != '') {
		$ids = implode(",", $_POST['checkall']);
		## check which action is selected
		$hidden_page = $_POST['hidden_pageno'];	
		if($_POST['action'] == 'active') {
			## Active selected records
			$blogObj->updateBlogStatus($ids, '1');
			$_SESSION['msg'] = "<div class='success_msg'><span>Blog status activated successfully</span></div>";
		} else if($_POST['action'] == 'inactive') {
			## Inactive selected records
			$blogObj->updateBlogStatus($ids, '0');
			$_SESSION['msg'] = "<div class='success_msg'><span>Blog status deactivated successfully</span></div>";
		} else {
			## Delete selected records			
			  if($ids!=''){
				 $blogid=explode(",",$ids);			  			  
				 foreach($blogid as $style){					 				
				 //$st=$objwStyle->getWheelStyleValueById($style);
				 $blogArray = $blogObj->getblogDetailsById($style);	
				 @unlink("../uploads/blog/".$blogArray['image']);				 				 
				}
			}
			$blogObj->deleteBlog($ids);
			$_SESSION['msg'] = "<div class='success_msg'><span>Blog deleted successfully</span></div>";
		}
		header('location:'.SITE_URL.'/admin/home.php?q=blogs&page='.$hidden_page);
		exit;
		## Delete code will be here if required
} // if close

## Active/Inactive/Delete thourgh URL
if(isset($_GET['action']) == 'status' && isset($_GET['status']) != '') {
	$id = base64_decode($_GET['id']);
	## Update blog page status 
	$array = array();
	$array['status'] = ($_GET['status']=='1'?0:1);
	$blogObj->editBlogById($array, $id);
	$_SESSION['msg'] = "<div class='success_msg'><span>Blog status updated successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=blogs&page='.$_GET['page']);
	exit;
}

if(isset($_GET['action']) == 'delete' && isset($_GET['id']) != '') {
	$id = base64_decode($_GET['id']);
	##Delete Blog Image
	$blogArray = $blogObj->getblogDetailsById($id);	
	@unlink("../uploads/blog/".$blogArray['image']);
	## Delete blog 
	$blogObj->deleteblog($id);
	$_SESSION['msg'] = "<div class='success_msg'><span>Blog deleted successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=blogs&page='.$_GET['page']);
	exit;
}
/*******************************/
if($_GET['q']=='blogs')
{
	$moduelArray=$moduleObj->getModuleHelpText(10);
	$smarty->assign('moduelArray', $moduelArray);
}
##Fetch all blog pages from database
## Get search parameters in variables - 
if($_POST['search'] != '' && $_POST['search'] != 'Search') {
	$searchindex = $_POST['search'];
} else if($_GET['search'] != '') {
	$searchindex = $_GET['search'];
} else {
	$searchindex = '';
}

// Assign search variable to show in search textbox
$smarty->assign('search', $searchindex);

if($_GET['col1'] != '') 
{
	$col1 = base64_decode($_GET['col1']);
} 
else if($_GET['col2'] != '') 
{
	$col2 = base64_decode($_GET['col2']);
} 
else if($_GET['col3'] != '') 
{
	$col3 = base64_decode($_GET['col3']);
} 
else 
{
	$col1 = '0';	
}

##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;
	if($_GET['col1'] != '') 
	{
		$pageName = "home.php?q=blogs&col1=".$_GET['col1'].""; ;
	} 
	else if($_GET['col2'] != '') 
	{
		$pageName = "home.php?q=blogs&col2=".$_GET['col2'].""; ;
	} 
	else if($_GET['col3'] != '') 
	{
		$pageName = "home.php?q=blogs&col3=".$_GET['col3'].""; ;
	} 
	else 
	{
		$pageName = "home.php?q=blogs"; 
	}
	

	
	## Count all the records
	$blogArray = $blogObj->getAllBlogs($searchindex,$col1,$col2,$col3);
	$total_rows = count($blogArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##

##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);
	$blogArray = $blogObj->getAllBlogs($searchindex,$col1,$col2,$col3, $rowsPerPage, $offset);
	$i=0;
	foreach($blogArray as $blogArray_rs) {
		$comment_count = $blogObj->getCommentCountByBlogId($blogArray_rs['id']);  // GET THE COMMENT COUNT 
		$blogArray[$i]['commentcount'] = $comment_count;
		
		$user_name = $userObj->getUserNameByUserId($blogArray_rs['user_id']);  // GET THE USER NAME 
		$blogArray[$i]['user_name'] = $user_name;
		
		$i++;
	}
	
//usage (only enter the keys you want sorted):
	
	if($col3=='0')
	{
		$blogArray = order_array_num($blogArray,'commentcount','ASC');
		$blogArray = @array_values($blogArray);
	}
	if($col3=='1')
	{
		$blogArray = order_array_num($blogArray,'commentcount','DESC');
		$blogArray = @array_values($blogArray);
	}

//print_r($sorted);

//echo "<pre>"; print_r($blogArray);exit;

	$smarty->assign('blogArray', $blogArray);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	} else {
		$other_id = '';
	}
	
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	

## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'blogs');
$smarty->assign('mainmenu', '3');

## Unset all the objects created which are on this page
unset($blogObj);
unset($userObj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'blogs/blogs.tpl');
unset($smarty);
?>